(* process.ml - Process image manipulations
 * Licence: LGPLv3
 * Authors: Franck "kushou" Michea < michea_f@epita.fr >
 *)

let ( |> ) x f = f x

let process = {
  Types.output = false;
  steps = Types.Base;
  show_steps = false
}

let add proc_n () = match proc_n with
    Types.Nothing -> ()
  | Types.ShowSteps -> process.Types.show_steps <- true
  | Types.Output -> process.Types.output <- true
  | Types.ImageType proc_name ->
    let foo = Process_types.get_process_level process.Types.steps
    and bar = Process_types.get_process_level proc_name in
    if foo < bar then process.Types.steps <- proc_name

let process_steps () =
  let call_fun img proc_name =
    (Process_types.get_process_fun proc_name) img in
  let get_result img proc_name = img#get proc_name in
  let process_list =
    Process_types.get_process_list process.Types.steps in
  let process_f img proc_name =
    call_fun img proc_name;
    if process.Types.show_steps then begin
      let n = Process_types.get_process_fun_name proc_name
      in if proc_name <> Types.RH then begin
	Printf.printf "[Image_functions.%s] Displaying resulting image.\n" n;
	Io.show img (get_result img proc_name) true end end in
  let f _ img =
    if process.Types.steps = Types.Base then ()
    else begin
      Printf.printf "-----\nBegining of processing on \"%s\" image.\n"
	img#get_filename;
      List.iter (process_f img) process_list;
      if not process.Types.show_steps then begin
	Printf.printf "Displaying result !\n";
	Io.show img (get_result img process.Types.steps) true end;
      Printf.printf "End of processing on \"%s\" image.\n" img#get_filename end
  in Image.iter f
